//
//  SEGTopicTextViewModel.m
//  Commom
//
//  Created by Clavis on 2019/7/29.
//

#import "SEGTopicTextViewModel.h"
#import <Commom/SEGColorManager.h>
#import <Commom/SEGLayoutMacro.h>

@implementation SEGTopicTextViewModel
-(instancetype)init{
    if (self == [super init]) {
        _content = @"";
        _topicTitle = @"";
        //        _bgColor = @"";
    }
    return self;
}
@end

/**************************SEGTopicTextViewLayout************************/
@implementation SEGTopicTextViewLayout
- (instancetype)initWithData:(SEGTopicTextViewModel *)data{
    if (self == [super init]) {
        _data = data;
    }
    [self segTopicTextViewLayout];
    return self;
}

-(void)segTopicTextViewLayout{
    //高度固定
    _totalHeight = kTopicTextViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _contentLabelHeight = 0;
    _contentLabelTextLayout = nil;
    _contentBgViewHeight = minContentHeight;
    _topicTitleLabelHeight = 23;
    _topicTitleLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    [self segcontentLabelWithContent:_data.content textColor:kColor_2];
    [self segtopicTitleLabelWithContent:[NSString stringWithFormat:@"%@",_data.topicTitle] fontSize:12 textColor:kColor_S3];
    _totalHeight = _contentBgViewHeight + 33;
}
//控件contentLabel文本布局计算
-(void)segcontentLabelWithContent:(NSString*)content textColor:(UIColor*)color{
    if(!content)return;
    
    CGFloat labelMaxWidth = _data.widgetWidth - contentXPadding - contentLabelLeftPadding;
    CGFloat fontSize = 15;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    
    CGFloat threeLineHeight = [self heightForString:@"一\n二\n三" font:font width:labelMaxWidth];
    CGFloat height = [self heightForString:content font:font width:labelMaxWidth];
    if (height > threeLineHeight) {//超过三行
        _data.isMoreLines = YES;
    } else {
        _data.isMoreLines = NO;
    }
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(labelMaxWidth, HUGE);
    container.linePositionModifier = modifier;
    
    _contentLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentLabelTextLayout)return;
    
    _contentLabelWidth = _contentLabelTextLayout.textBoundingSize.width;
    _contentLabelHeight = _contentLabelTextLayout.textBoundingSize.height;
    
    _contentViewHeight = _contentLabelHeight + contentLabelTopPadding;
    _contentBgViewHeight = _contentViewHeight + contentTopPadding + contentBottomPadding;
    if (_contentBgViewHeight < minContentHeight) {
        _contentBgViewHeight = minContentHeight;
    }
}

- (CGFloat)heightForString:(NSString *)string font:(UIFont *)font width:(CGFloat)width {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, MAXFLOAT)];
    label.text = string;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.bounds.size.height;
    return height;
}

//控件topicTitleLabel文本布局计算
-(void)segtopicTitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    //图片
    CGFloat padding = 3;
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_topic")];
    NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:image FontSize:image.size.height contentInsets:UIEdgeInsetsMake(0, 10, 0, padding)];
    if (img) {
        [text insertAttributedString:img atIndex:0];
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - 20, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 1;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd..."
    
    _topicTitleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //    if (!_topicTitleLabelTextLayout)return;
    //    _topicTitleLabelHeight = [modifier heightForLineCount:_topicTitleLabelTextLayout.rowCount];
    
    CGFloat w = [SEGKitHelper seg_sizeWithString:content fontSize:fontSize textColor:color].width;
    CGFloat labelWidth = w + padding + image.size.width + 20;
    _data.topicTitleWidth = labelWidth < _data.widgetWidth ? labelWidth : _data.widgetWidth;
}

@end
